<?php 

if (COUNT($_POST) > 0 && !$this->get('issearching'))
{
    $isreceiveorder = $this->get('isreceiveorder');
    $countprocessed = $this->get('countprocessed');
    $setor = $this->get('setor');
    Connection::getConnect();
	if ($setor)
	{
		$id = $this->get('id');
		$flag = $this->get('flag');
		$sql = "UPDATE reorder_response SET status=%d WHERE rotid=%d;";
		Connection::setQueryf($sql, $flag, $id);
		exit;
	}
	if ($countprocessed)
	{
		$sql = "SELECT COUNT(rrid) FROM reorder_response r WHERE status=1;";
		Connection::setQuery($sql);
		$count = Connection::fetch();
		echo $count[0];
		exit;
	}
    if ($isreceiveorder)
    {
    	$orders = $this->get('orders');
    	$total = $this->get('total');
		$rotid = $this->get('rotid');
		if (isset($orders) && COUNT($orders) > 0 && $orders)
		{
        	foreach($orders as $order){
        		$sql = "UPDATE item_inventory i SET quantity=quantity+" . $order[1] . "*(SELECT per_unit FROM supplier_item_cost s WHERE s.iid=" . $order[0] . " AND s.sdid=" . $order[2] . ") WHERE i.iid=" . $order[0];
    			Connection::setQuery($sql);
    			$sql = "UPDATE reorder_transaction r SET state=1 WHERE iid=" . $order[0] . " AND sdid=" . $order[2];
    			Connection::setQuery($sql);
        	}
    			$sql = "UPDATE reorder_response r SET status=10 WHERE (SELECT COUNT(rotid) FROM reorder_transaction rt WHERE rt.rotid=". $rotid . " AND state=0)=0 AND r.rotid=" . $rotid ;
    			Connection::setQuery($sql);
		}
    	exit;
    }
	$rotid = $this->get('rotid');
	$sql = "SELECT * FROM reorder_transaction JOIN reorder_response USING(rotid) JOIN supplier_details sd USING(sdid) WHERE rotid=$rotid AND state=0";
	Connection::setQuery($sql);
	$orders = Connection::fetchAll(MYSQL_ASSOC);
	$sql = "SELECT * FROM supplier_details WHERE sdid=".$orders[0]['sdid'];
	Connection::setQuery($sql);
	$supplier = Connection::fetch();
}else{
	Connection::getConnect();
	$key = $this->get('key');
	if($key != ""){
		$key = " WHERE id.name LIKE '%$key%' OR rt.rotid='$key'";
	}
	$sql = "SELECT *, sd.name sn FROM reorder_transaction rt JOIN supplier_details sd USING(sdid) JOIN reorder_response rr USING(rotid)  JOIN item_details id USING(iid) $key";
    Connection::setQuery($sql);
	$delist = Connection::fetchAll(MYSQL_ASSOC);
	
	function getStatus($flag=0){
		switch($flag){
			case 0:
				echo "Pending";
				break;
			case 1:
				echo "For Delivery";
				break;
			case 2:
				echo "Canceled";
				break;
			case 10:
				echo "Processed";
				break;
		}
	}
}

	function getDetail($iid, $pid, $qty)
	{
		$sql = "SELECT *,id.name as iname,sud.name as uname FROM supplier_item_cost sic
				JOIN item_details id USING(iid)
				JOIN supplier_details sd USING(sdid)
				JOIN item_unit_detail sud USING(imdid)
				WHERE sd.sdid = $pid AND id.iid = $iid";
		Connection::setQuery($sql);
		$suppl = Connection::fetch();
		echo "<tr class='list' iid='" . $suppl['iid'] . "' qty='" . $qty . "' imdid='" . $suppl['imdid'] . "' cost='" . $suppl['cost'] . "'>";
  		echo "	<td style='text-indent:5px;' class='bg ui-text-left'>" . $suppl['iname'] . "</td>";
  		echo "	<td>" . $suppl['uname'] . "(" . $suppl['per_unit'] . ") </td>";
  		echo "	<td class='bg'>" . $qty . "</td>";
  		echo "	<td>&#8369; " . number_format($suppl['cost'], 2, '.', ',') . "</td>";
		echo "	<td class='bg'><input type='button' class='brqty ui-ele-center'  value='receive' style='width: 60px;margin: 5px;' /><input style='margin-bottom:5px;width:35px;display:none' class='rqty ui-text-center' type='text' preval='" . $qty . "' value='" . $qty . "' /></td>";
  		//echo "	<td class='bg'>" . number_format($suppl['cost'] * $qty, 2, '.', ',') . "</td>";
  		echo "</tr>";
		return $suppl['cost'] * $qty;
	}
?>
